package com.example.jin.myapplication.beatbox;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.jin.myapplication.R;

/**
 * Created by jin on 2016/6/18.
 */
public class BeatBoxFragment extends Fragment {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private BeatBox mBeatBox;
    public static BeatBoxFragment newInstance(){
        return new BeatBoxFragment();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i("main","onDetach");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext=context;
        /**
         * Retain时会从析构的Activity中Detach然后重新Attach
         */
        Log.i("main","onAttach");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("main","onDestroyView");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * Fragment不会被析构
         * Fragment的View可以被析构并重建，但是Fragment不会
         *
         * 只保证Configure变化时Retain
         * 当系统回收内存时，Fragment还是会被析构，因此数据还是会被丢失
         *
         * Therefore, if you have something in your activity or fragment that should last a long time, then you
         should tie it to the activity record’s lifetime by overriding onSaveInstanceState(Bundle) to save its state so
         that you can restore it later.
         */
        setRetainInstance(true);
        mBeatBox=new BeatBox(getActivity());
        Log.i("main","onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i("main","onCreateView");
        View view=null;
        view=inflater.inflate(R.layout.fragment_beatbox,container,false);
        mRecyclerView=(RecyclerView)view.findViewById(R.id.beatbox_recyclerview);
        mRecyclerView.setAdapter(new BeatBoxAdapter());
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        return view;
    }
    private class BeatBoxHolder extends RecyclerView.ViewHolder{
        private View mItemView;
        public BeatBoxHolder(View itemView) {
            super(itemView);
            mItemView=itemView;
        }
        public void bind(final Sound sound){
            Button button=(Button)mItemView.findViewById(R.id.button_beatbox);
            button.setText(sound.getName());
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBeatBox.play(sound);
                }
            });
        }
    }
    private class BeatBoxAdapter extends RecyclerView.Adapter<BeatBoxHolder>{

        @Override
        public BeatBoxHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.item_beatbox,parent,false);
            return new BeatBoxHolder(view);
        }

        @Override
        public void onBindViewHolder(BeatBoxHolder holder, int position) {
            Sound sound=mBeatBox.getSoundList().get(position);
            holder.bind(sound);
        }

        @Override
        public int getItemCount() {
            return mBeatBox.getSoundList().size();
        }
    }

    @Override
    public void onDestroy() {
        /**
         * Retain为true时，旋转不会onDestroy函数
         * 但Activity退出时会调用这个函数
         */
        super.onDestroy();
        mBeatBox.release();
        Log.i("main","onDestroy");
    }
}
